<template>
  <div class="site-category">
    <el-card class="box-card">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm">
          <el-form-item>
            <el-input
              v-model="searchForm.categoryName"
              placeholder="分类名称"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button type="success" @click="handleAdd">新增分类</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="categoryList"
        border
        style="width: 100%"
        row-key="id"
      >
        <el-table-column prop="categoryName" label="分类名称" />
        <el-table-column prop="categoryIcon" label="图标">
          <template slot-scope="scope">
            <i :class="'el-icon-' + scope.row.categoryIcon"></i>
            {{ scope.row.categoryIcon }}
          </template>
        </el-table-column>
        <el-table-column prop="isSystem" label="系统分类" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isSystem ? 'success' : 'info'">
              {{ scope.row.isSystem ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sortOrder" label="排序" width="100" />
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
              :disabled="scope.row.isSystem"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              class="delete-btn"
              @click="handleDelete(scope.row)"
              :disabled="scope.row.isSystem"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" />
        </el-form-item>
        <el-form-item label="图标" prop="categoryIcon">
          <el-select v-model="form.categoryIcon" placeholder="请选择图标">
            <el-option
              v-for="icon in iconList"
              :key="icon"
              :label="icon"
              :value="icon"
            >
              <i :class="'el-icon-' + icon"></i>
              <span>{{ icon }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sortOrder">
          <el-input-number v-model="form.sortOrder" :min="0" :max="999" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryList,
  addCategory,
  updateCategory,
  deleteCategory
} from '@/api/site'

export default {
  name: 'SiteCategory',
  data() {
    return {
      // 搜索表单
      searchForm: {
        categoryName: ''
      },
      // 分页参数
      pageNo: 1,
      pageSize: 10,
      total: 0,
      // 数据列表
      categoryList: [],
      loading: false,
      // 对话框
      dialogVisible: false,
      dialogTitle: '',
      form: {
        id: null,
        categoryName: '',
        categoryIcon: '',
        sortOrder: 0
      },
      // 表单校验规则
      rules: {
        categoryName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        categoryIcon: [
          { required: true, message: '请选择图标', trigger: 'change' }
        ],
        sortOrder: [
          { required: true, message: '请输入排序号', trigger: 'blur' }
        ]
      },
      // 图标列表
      iconList: [
        'video-camera',
        'star',
        'document',
        'picture',
        'chat',
        'tools',
        'flag',
        'magic-stick',
        'guide',
        'tool',
        'like'
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取数据
    async fetchData() {
      this.loading = true
      try {
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          categoryName: this.searchForm.categoryName
        }
        const res = await getCategoryList(params)
        this.categoryList = res.data.data.rows
        this.total = res.data.data.total
      } catch (error) {
        console.error('获取分类列表失败:', error)
        this.$message.error('获取分类列表失败')
      }
      this.loading = false
    },
    // 搜索
    handleSearch() {
      this.pageNo = 1
      this.fetchData()
    },
    // 新增
    handleAdd() {
      this.dialogTitle = '新增分类'
      this.form = {
        id: null,
        categoryName: '',
        categoryIcon: '',
        sortOrder: 0
      }
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.dialogTitle = '编辑分类'
      this.form = { ...row }
      this.dialogVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该分类吗？', '提示', {
        type: 'warning'
      })
        .then(async () => {
          try {
            await deleteCategory(row.id)
            this.$message.success('删除成功')
            this.fetchData()
          } catch (error) {
            console.error('删除分类失败:', error)
            this.$message.error('删除分类失败')
          }
        })
        .catch(() => {})
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.form.id) {
              await updateCategory(this.form)
              this.$message.success('修改成功')
            } else {
              await addCategory(this.form)
              this.$message.success('新增成功')
            }
            this.dialogVisible = false
            this.fetchData()
          } catch (error) {
            console.error('保存分类失败:', error)
            this.$message.error('保存分类失败')
          }
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageNo = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
.site-category {
  padding: 20px;

  .search-bar {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }

  .delete-btn {
    color: #f56c6c;
  }

  .el-select {
    width: 100%;
  }

  .el-option {
    .el-icon {
      margin-right: 5px;
      font-size: 16px;
    }
  }
}
</style> 